#region License
//-----------------------------------------------------------------------------
// Copyright (c) 2008, Aaron MacDougall, Daniel Jeffery
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
//   this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
//   this list of conditions and the following disclaimer in the documentation
//   and/or other materials provided with the distribution.
//
// * Neither the name of Aaron MacDougall or Daniel Jeffery nor the names of its contributors may
//   be used to endorse or promote products derived from this software without
//   specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using System;
using System.Diagnostics;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
#endregion

namespace Agro2D
{
    /// <summary>
    /// Represents a named animation, called an Action. It inherits from IAnimation so that 
    /// it retains the interface of a normal animation.
    /// </summary>
    public class Action : IAnimation
    {
        public IAnimation animation;
        public string name;
        public Action() { }

        public Action(string name, IAnimation animation)
        {
            this.animation = animation;
            this.name = name;
        }

        public override bool Animate(GameTime gameTime, SpriteProperties spriteProperties)
        {
            return this.animation.Animate(gameTime, spriteProperties);
        }

        public override object Clone()
        {
            return new Action(this.name, (IAnimation)this.animation.Clone());
        }
    }

    /// <summary>
    /// A series of animations derived from IAnimation, each referred to by a string.
    /// A named animation is referred to as an Action.
    /// ActionAnimation is itself derived from IAnimation.
    /// </summary>
    public class ActionAnimation : IAnimation, IAnimationContainer
    {
        private Dictionary<string, Action> actions;
        private IAnimation currentAnim;
        private string currentActionName;
        private bool paused;
        private bool stopped;

        /// <summary>
        /// ActionAnimation constructor.
        /// </summary>
        public ActionAnimation()
        {
            this.actions = new Dictionary<string, Action>();
            this.currentAnim = null;
            this.currentAnim = null;
            this.paused = false;
            this.stopped = false;
        }

        /// <summary>
        /// Get the collection of actions.
        /// </summary>
        public Dictionary<string, Action> Actions
        {
            get { return this.actions; }
        }

        /// <summary>
        /// !ActionAnimation is a special case and returns null!
        /// </summary>
        public ICollection<IAnimation> Animations
        {
            get { return null; }
        }

        /// <summary>
        /// Gets the name of the animation that is currently playing.
        /// </summary>
        /// <returns>Name of the animation that is currently playing</returns>
        public string GetCurrentAnimationName()
        {
            return this.currentActionName;
        }

        /// <summary>
        /// Gets the animation that is currently playing.
        /// </summary>
        /// <returns>Animation that is currently playing</returns>
        public IAnimation GetCurrentAnimation()
        {
            return this.currentAnim;
        }

        /// <summary>
        /// Indicates whether the current animation has finished.
        /// </summary>
        /// <returns>true if current animation has stopped, false if otherwise</returns>
        public bool HasFinished()
        {
            return this.stopped;
        }

        /// <summary>
        /// Adds an animation to the available list that can be played.
        /// </summary>
        /// <param name="name">name to give the action</param>
        /// <param name="animation">animation to be added to the available animations</param>
        public void AddAction(string name, IAnimation animation)
        {
            Debug.Assert(name.Length > 0);
            Debug.Assert(animation != null);

            Action action = new Action(name, animation);
            actions.Add(action.name, action);
        }

        /// <summary>
        /// Adds an animation to the available list that can be played.
        /// </summary>
        /// <param name="action">Action to be added to the available actions, 
        /// containing name and animation</param>
        public void AddAction(Action action)
        {
            Debug.Assert(action != null);
            Debug.Assert(action.name.Length > 0);
            Debug.Assert(action.animation != null);

            actions.Add(action.name, action);
        }

        /// <summary>
        /// Makes an action active and start playing it.
        /// </summary>
        /// <param name="name">name of the action to play</param>
        /// <returns>true if the aniamtion can be played,
        /// false if the current animation hasn't stopped yet or
        /// if the named action is already playing or
        /// if the animation hasn't be added to ActionAnimation
        /// </returns>
        public bool PlayAction(string name)
        {
            Debug.Assert(name.Length > 0);

            // Make sure that the current animation isn't stopped
            if (this.currentActionName != name || this.stopped == true)
            {
                Action playAction = null;
                if(actions.ContainsKey(name))
                    playAction = actions[name];
                else
                    return false;

                this.currentAnim = (IAnimation)playAction.Clone();
                this.currentActionName = name;
                this.paused = false;
                this.stopped = false;
            }

            return true;
        }

        /// <summary>
        /// Plays the named action immediately
        /// </summary>
        /// <param name="name">name of the action to play</param>
        /// <returns>true if the animation can be played
        /// false if the animation hasn't be added to ActionAnimation</returns>
        public bool PlayActionNow(string name)
        {
            Debug.Assert(name.Length > 0);

            Action playAction = null;
            if(actions.ContainsKey(name))
                playAction = actions[name];
            else
                return false;

            this.currentAnim = (IAnimation)playAction.Clone();
            this.currentActionName = name;
            this.paused = false;
            this.stopped = false;           

            return true;
        }

        /// <summary>
        /// Plays the current animation if it has been stopped.
        /// </summary>
        public void PlayAction()
        {
            this.paused = false;

            if (this.stopped)
            {
                Action playAction = null;
                if(actions.ContainsKey(this.currentActionName))
                    playAction = actions[this.currentActionName];
                else
                    return;

                this.currentAnim = (IAnimation)playAction.Clone();
                this.stopped = false;
            }
        }

        /// <summary>
        /// Pauses the playing of the current animation. Use one of the Play methods to start the animation playing again.
        /// </summary>
        public void PauseAction()
        {
            this.paused = true;
        }

        /// <summary>
        /// Stops the current animation. Use one of the Play methods to start the animation playing again.
        /// </summary>
        public void StopAnimation()
        {
            this.stopped = true;
        }

        /// <summary>
        /// Updates the current animation with the current gameTime so that it can play.
        /// </summary>
        /// <param name="gameTime">game time</param>
        /// <param name="spriteProperties">spriteProperties of the sprite whom the animation belongs</param>
        /// <returns>always returns false for actionAnimation</returns>
        public override bool Animate(GameTime gameTime, SpriteProperties spriteProperties)
        {
            if (!(this.paused || this.stopped))
            {
                if (this.currentAnim != null)
                {
                    if (this.currentAnim.Animate(gameTime, spriteProperties))
                        this.stopped = true;
                }
            }

            return false;
        }

        /// <summary>
        /// Returns a cloned version of this object.
        /// </summary>
        /// <returns>Cloned version of this object.</returns>
        public override object Clone()
        {
            ActionAnimation anim = new ActionAnimation();
            foreach(string key in this.Actions.Keys)
            {
                Action action = this.Actions[key];
                anim.AddAction((Action)action.Clone());
            }

            return anim;
        }
    }
}
